/**
 *  TinyPasswordManager
 *  Copyright (C) 2008 Gross David <gdavid.devel@gmail.com>
 *                     Vinet Guillaume <vinet.guillaume@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "identifiertable.h"

#include <QMenu>
#include <QApplication>
#include <QAction>
#include <QHeaderView>
#include <QContextMenuEvent>
#include <QModelIndexList>
#include <QSignalMapper>

//
// Public
//

IdentifierTable::IdentifierTable(QWidget *parent)
        : QTableWidget(parent), m_translator(new QTranslator(NULL)) {
    m_copyAction = new QAction(tr("Copy password"), this);
	m_copyAction->setShortcut(Qt::CTRL | Qt::Key_C);
	
    m_editAction = new QAction(tr("Edit"), this);
	m_editAction->setShortcut(Qt::CTRL | Qt::Key_E);
	
    m_deleteAction = new QAction(tr("Delete"), this);
	m_deleteAction->setShortcut(Qt::Key_Delete);

    setIcons();
    createConnections();
    init();
}

IdentifierTable::~IdentifierTable()
{

}

Identifier IdentifierTable::getIdentifier(int index) {
    Identifier id("","","");

    if(index >= 0 && index < this->rowCount()) {
        id.setDescription(item(index,0)->text().toStdString());
        id.setLogin(item(index,1)->text().toStdString());
        id.setPassword(item(index,2)->text().toStdString());
    }

    return id;
}

void IdentifierTable::setIdentifier(int index, const Identifier &id) {
    if(index >= 0 && index < this->rowCount()) {
        setItem(index, 0, new QTableWidgetItem(QString::fromStdString(id.getDescription())));
        setItem(index, 1, new QTableWidgetItem(QString::fromStdString(id.getLogin())));
        setItem(index, 2, new QTableWidgetItem(QString::fromStdString(id.getPassword())));
    }
}

void IdentifierTable::addIdentifier(Identifier id) {
    int rows = rowCount();
    setRowCount(rows+1);
    setIdentifier(rows, id);
}

void IdentifierTable::removeIdentifier(int index) {
     if(index >= 0 && index < this->rowCount())
         removeRow(index);
}

//
// Protected
//

void IdentifierTable::contextMenuEvent(QContextMenuEvent *event) {
    if(selectedIndexes().count() > 0) {
         QMenu menu(this);
         menu.addAction(m_copyAction);
         menu.addAction(m_editAction);
         menu.addAction(m_deleteAction);
         menu.exec(event->globalPos());
    }
}

void IdentifierTable::keyPressEvent(QKeyEvent *event) {
    emit keyEvent(event);
}

//
// Private
//

void IdentifierTable::createConnections() {
    connect(m_copyAction, SIGNAL(triggered()), this, SIGNAL(copyPassword()));
    connect(m_editAction, SIGNAL(triggered()), this, SIGNAL(edit()));
    connect(m_deleteAction, SIGNAL(triggered()), this, SIGNAL(remove()));
}

void IdentifierTable::setIcons() {
    m_copyAction->setIcon(QIcon(":images/copy"));
    m_editAction->setIcon(QIcon(":images/edit"));
    m_deleteAction->setIcon(QIcon(":images/delete"));
}

void IdentifierTable::init() {
    setColumnCount(3);

    QStringList labels;
    labels << tr("Description") << tr("Login") << tr("Password");
    setHorizontalHeaderLabels(labels);
    verticalHeader()->setHidden(true);
    horizontalHeader()->setHidden(false);
    horizontalHeader()->setSortIndicatorShown(true);

    horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
    horizontalHeader()->setResizeMode(2, QHeaderView::Stretch);
}

void IdentifierTable::translate(const QString &language) {
    if(m_translator)
        qApp->removeTranslator(m_translator);
        
    if(language != "uk") {
        m_translator->load("i18n_" + language + ".qm", ":/translations/");
        qApp->installTranslator(m_translator);
    }
    
    m_copyAction->setText(tr("Copy password"));
    m_editAction->setText(tr("Edit"));
    m_deleteAction->setText(tr("Delete"));
	QStringList labels;
    labels << tr("Description") << tr("Login") << tr("Password");
    setHorizontalHeaderLabels(labels);
}

